/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.boundary;

import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Group;
import it.polimi.registration.business.security.entity.Location;
import it.polimi.registration.business.security.entity.Users;
import it.polimi.registration.business.security.entity.UsersEvent;
import java.util.Date;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 *
 * @author Luca
 */
@RunWith(Arquillian.class)
public class UserEventManagerIT {
    @EJB
    UsersManager cut;

    @PersistenceContext
    EntityManager em;
    
    @EJB
    EventManager evm;
    
    @EJB
    UsersEventManager uem;
    
    @Deployment
    public static WebArchive createArchiveAndDeploy() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(UsersManager.class)
                .addClass(EntityManager.class)
                .addClass(EventManager.class)
                .addPackage(Event.class.getPackage())
                .addPackage(Users.class.getPackage())
                .addPackage(UsersEvent.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
    
    @Test
    public void newEventsShouldBeValid() {
        Users newUsers = new Users();
        newUsers.setEmail("invalidemail@mail.it");
        newUsers.setGroupName(Group.USERS);
        newUsers.setName("name");
        newUsers.setPassword("password");
        cut.save(newUsers);
        Event newEvent = new Event();
        newEvent.setCreator(newUsers);
        newEvent.setEndDate(new Date());
        newEvent.setEndH(new Date());
        newEvent.setId(Long.MAX_VALUE);
        newEvent.setLocation(new Location());
        newEvent.setPreference("true");
        newEvent.setStartDate(new Date());
        newEvent.setStartH(new Date());
        newEvent.setTitle("ciao");
        evm.save(newEvent);
        UsersEvent usev= new UsersEvent();
        usev.setAccepted(Boolean.FALSE);
        usev.setCreator(Boolean.TRUE);
        usev.setEvent(newEvent);
        usev.setId(newEvent.getId());
        usev.setSeen(Boolean.TRUE);
        usev.setUsers(newUsers);
        Event ev = em.find(Event.class, newEvent.getId());
        Users us=em.find(Users.class, "invalidemail@mail.it");
        UsersEvent ue = em.find(UsersEvent.class, usev.getId());
        assertEquals(ue.getId(),usev.getId());
    }
}
